﻿'========================================================================
'功能:變更項目/限撥參數設定
'作者:Shihyu
'日期: 2011/5/13
'========================================================================
Partial Class WebForm_Manage_ModifyItemMaintain
    Inherits BasePage
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            hiUID.Value = ActualUID
            hiDBName.Value = dbName
            If Not ChkProgramRight(1) Then '查詢
                btnInsert.Visible = False
                gvMODIFYItem.visible = False
            End If
            If Not ChkProgramRight(2) Then '新增
                btnInsert.Visible = False

            End If


            If Not ChkProgramRight(2) Then '編輯
                gvMODIFYItem.Columns(0).Visible = False
                gvMODIFYItemKind.Columns(0).Visible = False
            End If


        End If

    End Sub


    Protected Sub btnInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsert.Click
        hiKind.Value = "Insert"
        gvMODIFYItem.SelectedIndex = -1
        gvMODIFYItem.EditIndex = 0
        gvMODIFYItem.PageIndex = 0
        gvMODIFYItem.DataBind()

        gvMODIFYItemKind.Visible = False
        btnInsertDetail.Visible = False
    End Sub


    Protected Sub btnInsertDetail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnInsertDetail.Click
        Me.mpSearch.Show()
        hiKindDetail.Value = "InsertNew"

        gvMODIFYItemKind.SelectedIndex = -1
        gvMODIFYItemKind.EditIndex = 0
        gvMODIFYItemKind.PageIndex = 0

        gvMODIFYItemKind.DataBind()
    End Sub


    Protected Sub gvMODIFYItemKind_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvMODIFYItemKind.RowCancelingEdit
        gvMODIFYItemKind.EditIndex = -1
        hiKindDetail.Value = ""
    End Sub

    Protected Sub gvMODIFYItem_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles gvMODIFYItem.RowCancelingEdit
        gvMODIFYItem.EditIndex = -1
        hiKind.Value = ""
    End Sub

    Protected Sub gvMODIFYItem_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvMODIFYItem.RowCommand
        If e.CommandName = "Select" Then
            gvMODIFYItem.EditIndex = -1 '取消編輯模式
            Me.mpSearch.Show()
            '將對應的Gridview顯示
            gvMODIFYItemKind.Visible = True
            hiModifyItem_code.Value = e.CommandArgument.ToString()
            btnInsertDetail.Visible = ChkProgramRight(2)
        End If

        If e.CommandName = "Edit" Then
            btnInsertDetail.Visible = False
            gvMODIFYItemKind.Visible = False
        End If
    End Sub

    Protected Sub gvMODIFYItem_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles gvMODIFYItem.RowUpdated
        gvMODIFYItem.EditIndex = -1
        hiKind.Value = ""
    End Sub


    Protected Sub gvMODIFYItemKind_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvMODIFYItemKind.RowDeleting
        Me.mpSearch.Show()
        Dim ModifyItem_code As String = CType(gvMODIFYItemKind.Rows(e.RowIndex).FindControl("lblKindModifyItem_code"), Label).Text.Trim
        Dim type_code As String = CType(gvMODIFYItemKind.Rows(e.RowIndex).FindControl("lblkind_code"), Label).Text.Trim
        Dim bbl As Telecom.BBL.ModifyItemBLL = New Telecom.BBL.ModifyItemBLL()
        bbl.DeleteMODIFYItemKind(dbName, Me.ddlsuit_site.SelectedValue.Trim, ModifyItem_code, type_code) '刪除對應資料
    End Sub


    Protected Sub gvMODIFYItemKind_RowUpdated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdatedEventArgs) Handles gvMODIFYItemKind.RowUpdated
        gvMODIFYItemKind.EditIndex = -1
        hiKindDetail.Value = ""
    End Sub

    Protected Sub ddlsuit_site_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlsuit_site.SelectedIndexChanged
        hiKind.Value = ""
        gvMODIFYItem.EditIndex = -1
        gvMODIFYItem.DataBind()

        If gvMODIFYItemKind.Visible = True Then '如果對應之GridView顯示則取消編輯模式
            gvMODIFYItemKind.EditIndex = -1
            gvMODIFYItemKind.DataBind()
        End If
    End Sub

    Protected Sub gvMODIFYItem_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvMODIFYItem.RowUpdating
        Dim ModifyItem_code As String = CType(gvMODIFYItem.Rows(e.RowIndex).FindControl("txtModifyItem_code"), TextBox).Text.Trim
        Dim ModifyItem_name As String = CType(gvMODIFYItem.Rows(e.RowIndex).FindControl("txtModifyItem_name"), TextBox).Text.Trim
        Dim ModifyItem_desc As String = CType(gvMODIFYItem.Rows(e.RowIndex).FindControl("txtModifyItem_desc"), TextBox).Text.Trim
        'Dim OrginalData As String = CType(gvMODIFYItem.Rows(e.RowIndex).FindControl("txtOrginalData"), TextBox).Text.Trim
        Dim NeedApproveText As String = CType(gvMODIFYItem.Rows(e.RowIndex).FindControl("txtNeedApprove"), TextBox).Text.Trim
        Dim KIND As String = CType(gvMODIFYItem.Rows(e.RowIndex).FindControl("ddlKind"), DropDownList).SelectedValue.Trim
        Dim Message As String = CType(gvMODIFYItem.Rows(e.RowIndex).FindControl("txtMessage"), TextBox).Text.Trim
        Dim NeedApprove As Integer
        '判斷輸入的值是否為數字,若不是將該值預設=7
        If IsNumeric(NeedApproveText) = True Then
            NeedApprove = Int32.Parse(NeedApproveText)
        Else
            NeedApprove = 7
        End If

        '檢查代號是否空白
        If ModifyItem_code = "" Then
            e.Cancel = True
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("ErrEmpty"))
            Exit Sub
        End If

        '判斷名稱是否空白
        If ModifyItem_name = "" Then
            e.Cancel = True
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("NameErrEmpty"))
            Exit Sub
        End If

        '判斷說明是否空白
        If ModifyItem_desc = "" Then
            e.Cancel = True
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("DescErrEmpty"))
            Exit Sub
        End If

 

        '判斷種類是否空白
        If KIND = "" Then
            e.Cancel = True
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("KindErrEmpty"))
            Exit Sub
        End If

        '判斷提醒語是否空白

        'If Message = "" Then
        '    e.Cancel = True
        '    ShowMessageForAjax(btnInsert, GetLocalResourceObject("MessageErrEmpty"))
        '    Exit Sub
        'End If


        '檢查簽核層級只可輸入0~6
        If NeedApprove < 0 OrElse NeedApprove > 6 Then
            e.Cancel = True
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("ErrNumber"))
            Exit Sub
        End If

        '判斷代號是否存在
        Dim bbl As Telecom.BBL.ModifyItemBLL = New Telecom.BBL.ModifyItemBLL()
        If CType(gvMODIFYItem.Rows(e.RowIndex).FindControl("txtModifyItem_code"), TextBox).Enabled Then
            If bbl.MODIFYItemIsExist(dbName, ddlsuit_site.SelectedValue, ModifyItem_code) Then
                e.Cancel = True
                ShowMessageForAjax(btnInsert, GetLocalResourceObject("ErrExist"))
                Exit Sub
            End If
        End If
    End Sub

    Protected Sub gvMODIFYItemKind_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvMODIFYItemKind.RowUpdating
        Me.mpSearch.Show()
        Dim ModifyItem_code As String = CType(gvMODIFYItemKind.Rows(e.RowIndex).FindControl("lblKindModifyItem_code"), Label).Text.Trim
        Dim kind_code As String = CType(gvMODIFYItemKind.Rows(e.RowIndex).FindControl("ddlMODIFY_KIND"), DropDownList).SelectedValue.Trim

        '檢查變更項目對應代號是否空白
        If ModifyItem_code = "" Then
            e.Cancel = True
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("ModifyItemcodeErrEmpty"))
            Exit Sub
        End If

        '檢查申請項目代號是否空白
        If kind_code = "" Then
            e.Cancel = True
            ShowMessageForAjax(btnInsert, GetLocalResourceObject("ErrEmptytype_code"))
            Exit Sub
        End If

        '判斷變更項目對應代號是否存在
        Dim bbl As Telecom.BBL.ModifyItemBLL = New Telecom.BBL.ModifyItemBLL()
        If ModifyItem_code <> "" Then
            If bbl.MODIFYItemKindIsExist(dbName, ddlsuit_site.SelectedValue, ModifyItem_code, kind_code) Then
                e.Cancel = True
                ShowMessageForAjax(btnInsert, GetLocalResourceObject("ModifyItemcodeErrExist"))
                Exit Sub
            End If
        End If
    End Sub
End Class
